<?
include "libhtml.php";
include "libsession.php";
requireLogin();
echoHeader("Edit Document");

// get the docid from the params
if (isset($_GET["docid"]))
{
	$docid = $_GET["docid"];	
}
else
{
	$docid = 0;	
}

if ($docid > 0)
{
	// load the page
	$sql = "SELECT d.Id, d.Title, u.Id AS OwnerId, u.Name AS OwnerName, 
				d.DateDue, d.WorkflowTemplateId, 
				ws.Id AS StepId, ws.Name AS StepName, d.FileName, ws.IsLocked
			FROM Document d
			LEFT JOIN User u ON u.Id=d.OwnerId
			LEFT JOIN WorkflowStep ws ON ws.Id=d.WorkflowStepId
			WHERE d.Id=$docid
			LIMIT 1";
			
	$result = query_db($sql, MYSQL_ASSOC);
	if ($result != null && mysql_num_rows($result) == 1)
	{
		$document = mysql_fetch_array($result);
		mysql_free_result($result);
	}
	else
	{
		// clear the id to trigger a new document
		$docid=null;
	}
}

// get a list of users
$sql = "SELECT Id, Name FROM User";
$users = query_db($sql);

// get a list of workflow templates
$sql = "SELECT Id, Name FROM WorkflowTemplate";
$templates = query_db($sql);

?>

<h1>Edit Document </h1>
<form action='WorkflowUpdate.php' method='post' enctype="multipart/form-data">
	<input type=hidden name='action' value='updatedocument'>
	<input type=hidden name=targetid value=<? echo $document["Id"]; ?>>
	<input type=hidden name=redirecturl value='Document_Edit.php?docid=<? echo $document["Id"]; ?>'>
	<input type=hidden name=oldtemplateid value=<? echo $document["WorkflowTemplateId"]; ?>>
<table border="0" cellpadding="0" cellspacing="0">
  <tbody>
    <tr>
      <td class='leftHeader'>Title:<br>
      </td>
      <td><input type='text' size='100' name="Title" value='<? echo $document["Title"]?>' ></td>
    </tr>
    <tr>
      <td class='leftHeader'>Owner<br></td>
      <td> <? echo $document["OwnerName"]; ?></td>
    </tr>
    <tr>
      <td class='leftHeader'>Date Due:<br>
      </td>
      <td><input type='text' name="DateDue" value='<? echo $document["DateDue"]; ?>' size=8> (YYYY-MM-DD)<br>
      </td>
    </tr>
    <tr>
    	<td class='leftHeader'>Workflow Template:</td>
    	<td>
    		<select name=WorkflowTemplateId>
    			<option value=0></option>
    		<?
    			// reset the template list
    			mysql_data_seek($templates, 0);
    			
    			while ($template = mysql_fetch_array($templates, MYSQL_ASSOC))
    			{
	    			$isSelected = "";
					if ($template["Id"] == $document["WorkflowTemplateId"])
						$isSelected = " selected='selected'";
					echo "<option value=" . $template["Id"] . $isSelected . ">" . $template["Name"] . "</option>";
    			}
    		?>
    		</select>
    	</td>
    </tr>
    <tr>
      <td class='leftHeader'>Current Workflow Step<br>
      </td>
      <td><? echo $document["StepName"] ?><br>
      </td>
    </tr>
    <tr>
    	<input type=hidden name=CurrentFile value='<? echo $document["FileName"]; ?>'>
    	<td class='leftHeader'>Current File:</td>
    	<td><? echo $document["FileName"]; ?></td>
    </tr>
    <tr>
		<td class='leftHeader'>Upload New File:<br></td>
		<td>
    		<?
    			if ($document["IsLocked"] == 1)
    			{
					echo "File is locked and cannot be changed.";
				}
				else
				{	
					echo "<input type='file' name='file' id='file'/> ";
				}
    		?>
		</td>
    </tr>
    <tr>
      <td class='leftHeader'>Search file for:<br>
      </td>
      <td><input type='text' size='100' name="txtSearchQuery"><br>
      </td>
    </tr>
  </tbody>
</table>
<br>
<div style='text-align: center;'>
	<input type='submit' value='Save' />
</div>
</form>
<?
	if ($document["FileName"] != null && $document["FileName"] != '')
	{
		echo "<form action='WorkflowUpdate.php' method='post' enctype='multipart/form-data'>";
		echo "	<input type=hidden name='action' value='downloadfile'>";
		echo "	<input type=hidden name=targetid value=" . $document["Id"] . ">";
		echo "	<input type=hidden name=redirecturl value='Document_Edit.php?docid=" . $document["Id"] ."'>";
		echo "	<input type=submit name=submit value='Download File'>";
		echo "</form>";
	}
?>
<table>
	
<tr>
      <td class='leftHeader'>Workflow Step Click Actions:<br>
      </td>
      <td>
			<?
				// find all the click events for this step
				if ($document["StepId"] != 0)
				{

					$stepid = $document["StepId"];
						
					$sql = "SELECT wa.Id, wa.Type, 
								wa.ParamStepId AS StepId, ws.Name AS StepName,
								wa.ParamUserId AS UserId, u.Name AS UserName
							FROM WorkflowAction wa
							LEFT JOIN User u ON u.Id=wa.ParamUserId
							LEFT JOIN WorkflowStep ws ON ws.Id=wa.ParamStepId
							WHERE wa.StepId=$stepid AND wa.EventType=" . WF_EVENT_TYPE_Click . "
							ORDER BY wa.Id;";

					$results = query_db($sql);
					if ($results != null && mysql_num_rows($results) > 0)
					{
						
						while ($action = mysql_fetch_array($results, MYSQL_ASSOC))
						{
							// figure out what the button should say
							$buttontext = "";
							if ($action["Type"] == WF_ACTION_TYPE_ChangeOwner && $action["UserId"] > 0)
							{
								$buttontext = "Change owner to: " . $action["UserName"];
							}
							else if ($action["Type"] == WF_ACTION_TYPE_SendEmail && $action["UserId"] > 0)
							{
								$buttontext = "Send Email to: " . $action["UserName"];
							}
							else if ($action["Type"] == WF_ACTION_TYPE_Navigate && $action["StepId"] > 0)
							{
								$buttontext = "Change to step: " . $action["StepName"];
							}
							
							// only show a button for valid actions
							if ($buttontext != "")
							{
								$actionid = $action["Id"];
								echo "<form action=WorkflowUpdate.php method=post>";
								echo "	<input type=hidden name='action' value='executeaction'>";
								echo "	<input type=hidden name=targetid value=$actionid>";
								echo "	<input type=hidden name=redirecturl value='Document_Edit.php?docid=$docid'>";
								echo "	<input type=hidden name=docid value=$docid>";
								echo "	<input type=submit value='$buttontext'>";
								echo "</form>";
							}
						}
					}
				}	
			?>
      </td>
    </tr>
</table>

<? echoFooter(); ?>